{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "15239f4b",
   "metadata": {},
   "source": [
    "## 函数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e34aa085",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a =10\n",
    "b =20\n",
    "a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "82eede8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=  10\n",
      "a=  10\n",
      "a=  10\n"
     ]
    }
   ],
   "source": [
    "def print_value():\n",
    "    print(\"a= \",a)\n",
    "    print(\"a= \",a)\n",
    "    print(\"a= \",a)\n",
    "    \n",
    "print_value()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "805e4417",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dddd\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def add_ab(a,b):\n",
    "    print('dddd')\n",
    "    return (a+b)\n",
    "    \n",
    "tang= add_ab(3,5)\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "24b1cce7",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "add_ab() missing 1 required positional argument: 'b'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[8], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m tang \u001b[38;5;241m=\u001b[39m add_ab(\u001b[38;5;241m3\u001b[39m,)\n",
      "\u001b[1;31mTypeError\u001b[0m: add_ab() missing 1 required positional argument: 'b'"
     ]
    }
   ],
   "source": [
    "tang = add_ab(3,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f348ee2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def add_ab(a=1,b=2):\n",
    "    return (a+b)\n",
    "tang = add_ab(3,3)\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "46cce886",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang = add_ab(a=3,b=3)\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8f5502f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15 34\n"
     ]
    }
   ],
   "source": [
    "def add_number(a,*args):\n",
    "    b =0\n",
    "    for i in args:\n",
    "        a +=i\n",
    "        b +=a\n",
    "    return a,b\n",
    "\n",
    "a,b=add_number(1,2,3,4,5)\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f2f20748",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x 2\n",
      "y 3\n"
     ]
    }
   ],
   "source": [
    "def add_number2(a,**kwargs):\n",
    "    for arg,value in kwargs.items():\n",
    "        print(arg,value)\n",
    "add_number2(1,x=2,y=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a90476e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
